#include "Log.h"
#include "Connect.h"
#include "Task.h"

namespace sevent{

Task::Task(Server *server, Connect *connect)
:_parentServer(server), _connect(connect){
}

Task::Task(Server *server, Connect *c, const TaskFunc &f)
:_parentServer(server), _connect(c), _runFunc(f){
}

Task::~Task(){
	_connect->subCount();
}

int Task::execute(){
	LOG_DEBUG_VA("Task %x start execute", this);
	int ret;
	try{
	_runFunc(_connect);
	}catch(exception &e) {
		std::cerr << "catch exception: " << e.what() << std::endl;
	}

	LOG_DEBUG_VA("Task %x execute over", this);
	return ret;
}

/*
int Task::execute(){
	LOG_DEBUG_VA("Task %x start execute", this);
	char *buf = new char[LEN];
	int len = 0;
	len = _connect->read(buf, LEN, 10000);
	//len = ::read(_connect->fd(), buf, LEN);
	if(len == 0){
		LOG_DEBUG_VA("Task %x read 0 bytes, going to close this Connect", this, len);
		_connect->close();
		return 0;
	}
	LOG_DEBUG_VA("Task %x read %d bytes from %x:%d", this, len, _connect, _connect->fd());
	//do something
	
	_connect->write(buf, LEN, 10000);
	//len = ::write(_connect->fd(), buf, LEN);
	delete [] buf;
	LOG_DEBUG_VA("Task %x write %d bytes", this, len);
	LOG_DEBUG_VA("Task %x execute over", this);
	return 0;
}
*/

}


